package com.dk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dk.mapper.PersonRoleMapper;
import com.dk.mapper.TPersonMapper;
import com.dk.pojo.PersonRoleExample;
import com.dk.pojo.TPerson;
import com.dk.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private TPersonMapper tPersonMapper;

    @Autowired
    private PersonRoleMapper personRoleMapper;

    @Override
    public List<TPerson> getInfo() {
        return tPersonMapper.selectByExample(null);
    }

    @Override
    public Boolean deleteInfo(Integer pid) {
        try{
            //删除用户表
            tPersonMapper.deleteByPrimaryKey(pid);
            //删除用户对应的角色信息
            PersonRoleExample ure = new PersonRoleExample();
            PersonRoleExample.Criteria criteria = ure.createCriteria();
            criteria.andPersonIdEqualTo(pid);
            personRoleMapper.deleteByExample(ure);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public boolean addInfo(TPerson tPerson, Integer[] roleId) {

        try {
            tPerson.setPwd("1234");
            tPerson.setSalt("0");
            //增加用户表返回主键
            int i = tPersonMapper.insertInfoReturnId(tPerson);
            //增加中间表
            Integer personId = tPerson.getPid();
            int i2 = personRoleMapper.insertBatch(personId,roleId);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public TPerson getInfoById(Integer pid) {
        return tPersonMapper.selectByPrimaryKey(pid);
    }

    @Override
    public boolean updateInfo(TPerson tPerson, Integer[] roleId) {
        try{
            //修改用户信息
            tPersonMapper.updateInfo(tPerson);
            //删除中间表
            PersonRoleExample ure = new PersonRoleExample();
            PersonRoleExample.Criteria criteria = ure.createCriteria();
            criteria.andPersonIdEqualTo(tPerson.getPid());
            personRoleMapper.deleteByExample(ure);
            //批量增加中间表
            Integer personId =  tPerson.getPid();
            personRoleMapper.insertBatch(personId,roleId);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public Integer getById(TPerson tPerson, String s, String vcode) {
        if(!s.equalsIgnoreCase(vcode)){
            return 5;
        }
        TPerson u = tPersonMapper.getById(tPerson.getPname());
        if(u == null){
            return 1;
        }

        if (!u.getPwd().equals(tPerson.getPwd())){
            return 3;
        }

        return 4;
    }

    @Override
    public TPerson getByName(String pname) {
        return tPersonMapper.getById(pname);
    }
}
